package sf.database.dialect.db;

import sf.database.dialect.Keywords;
import sf.querydsl.QueryDSLSupportDatabase;

/**
 * sqlserver2012
 */
public class SQLServer2012Dialect extends SQLServer2008Dialect {

    @Override
    public String getQueryDslDialect() {
        return QueryDSLSupportDatabase.SQLServer2012;
    }

    public SQLServer2012Dialect() {
        this.keywords.addAll(Keywords.SQLSERVER2008);
    }

    @Override
    public StringBuilder sqlPageList(StringBuilder sql, long offset, int limit) {
        //sqlserver 2012 以上的 offset 分页必须要跟在order by后面，因此如果语句本身没有order by则为其添加一个按默认时间戳的order by
        if (!sql.toString().matches("(?i).* order by[^)]+$")) {
            sql.append(" order by current_timestamp");
        }
        if (offset < 0) {
            offset = 0;
        }
        sql.append(" OFFSET ").append(offset).append(" ROW FETCH NEXT ").append(limit).append(" rows only");
        return sql;
    }
}
